import request from "@/utils/newRequest";
const pokerURL = process.env.VUE_APP_WEPOKER_API;

// Import the required MD5 library
import CryptoJS from 'crypto-js';
import axios from "axios";

// Set additional custom headers from REQ_Headers
const REQ_Headers = {
  "accept": "*/*",
  "accept-encoding": "gzip, deflate, br",
  "content-length": "183", // Note: content-length will be auto-calculated by Axios
  "content-type": "application/x-www-form-urlencoded",
  "origin": "https://h5.pinqiaolx.com",
  "referer": "https://h5.pinqiaolx.com/",
  "sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="101"',
  "sec-ch-ua-mobile": "?0",
  "sec-ch-ua-platform": '"Windows"',
  "sec-fetch-dest": "empty",
  "sec-fetch-mode": "cors",
  "sec-fetch-site": "cross-site",
  // Setting user-agent dynamically using navigator.userAgent
  "user-agent": navigator.userAgent
};

// Function to generate a signature based on sorted parameters
function genParamsSign(dictParams, salt = "110") {
  console.log("gen_params_sign params :", dictParams);

  // Sort the parameters keys
  const sortedKeys = Object.keys(dictParams).sort();
  let cleanedString = '';

  // Generate the cleaned string by concatenating key-value pairs
  for (const key of sortedKeys) {
    cleanedString += `${key}${dictParams[key]}`;
  }

  // Remove special characters
  cleanedString = cleanedString.replace(/[^a-zA-Z0-9]/g, '');

  // Add salt and create the final string
  const finalString = `${salt}${cleanedString}${salt}`;

  // Generate MD5 hash using CryptoJS and convert to upper case
  const md5Hash = CryptoJS.MD5(finalString).toString().toUpperCase();
  console.log("gen_params_sign sign :", md5Hash);
  return md5Hash;
}

// Function to generate a hashed password
function genMD5Password(password) {
  const newp = `${password}49C764D98E177F70`;
  return CryptoJS.MD5(newp).toString().toUpperCase();
}


// 获取公钥
export const getPublicKey = async (data) => {
  // Generate the sign based on the input data
  const sign = genParamsSign(data);

  // Add the sign to the data object
  const requestData = {
    ...data,
    sign: sign
  };

  try {
    const response = await axios.post(`${pokerURL}/system/getUserRASPubKey`, requestData, {
      headers: {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "content-length": "183", // Note: content-length will be auto-calculated by Axios
        "content-type": "application/x-www-form-urlencoded",
        "origin": "https://h5.pinqiaolx.com",
        "referer": "https://h5.pinqiaolx.com/",
        "sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="101"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"Windows"',
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "cross-site",
        // Setting user-agent dynamically using navigator.userAgent
        "user-agent": navigator.userAgent
      }
    });

    return response.data;
  } catch (error) {
    console.error("请求错误:", error);
    throw error; // 或根据需求处理错误
  }
    // return request({
    //   url: `${pokerURL}/system/getUserRASPubKey`,
    //   method: "post",
    //   requestData,
    // });
}
